/*
 * PanelBusquedaSimple.java
 *
 * Created on 19 de diciembre de 2007, 19:58
 */
package com.codeko.apps.campanilla.ignotus;

import com.codeko.swing.CodekoTableModel;
import com.codeko.swing.SwingUtil;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author  campanilla
 */
public class PanelTablaLibros extends javax.swing.JPanel {

    CodekoTableModel<Libro> modelo = new CodekoTableModel<Libro>(new Libro());

    /** Creates new form PanelBusquedaSimple */
    public PanelTablaLibros() {

        initComponents();
        buscador.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if ("nuevaBusqueda".equals(evt.getPropertyName())) {
                    modelo.vaciar();
                } else if ("addLibro".equals(evt.getPropertyName())) {
                    modelo.addDato((Libro) evt.getNewValue());
                } else if ("busquedaTerminada".equals(evt.getPropertyName())) {
                    tabla.packAll();
                }
            }
        });
        SwingUtil.addMenuTabla(tabla, true, "Ignotus", "Listado de libros");
        tabla.getSelectionModel().addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                listaSeleccionada();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lBuscar = new javax.swing.JLabel();
        tfBuscar = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new org.jdesktop.swingx.JXTable();

        setName("Form"); // NOI18N
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });
        addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                formAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.codeko.apps.campanilla.ignotus.IgnotusApp.class).getContext().getResourceMap(PanelTablaLibros.class);
        lBuscar.setText(resourceMap.getString("lBuscar.text")); // NOI18N
        lBuscar.setToolTipText(resourceMap.getString("lBuscar.toolTipText")); // NOI18N
        lBuscar.setName("lBuscar"); // NOI18N

        tfBuscar.setText(resourceMap.getString("tfBuscar.text")); // NOI18N
        tfBuscar.setName("tfBuscar"); // NOI18N
        tfBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfBuscarKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tfBuscarKeyTyped(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabla.setAutoCreateRowSorter(true);
        tabla.setModel(modelo);
        tabla.setColumnControlVisible(true);
        tabla.setName("tabla"); // NOI18N
        jScrollPane1.setViewportView(tabla);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lBuscar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfBuscar, javax.swing.GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lBuscar)
                    .addComponent(tfBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tfBuscarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfBuscarKeyTyped
        buscador.buscar(tfBuscar.getText() + evt.getKeyChar());
    }//GEN-LAST:event_tfBuscarKeyTyped
    private void cerrar() {
        SwingUtilities.getWindowAncestor(this).dispose();
    }

    private void listaSeleccionada() {
        int pos = tabla.getSelectedRow();
        if (pos > -1) {
            Object val = modelo.getElemento(pos);
            if (val != null && val instanceof Libro) {
                ((IgnotusView) IgnotusApp.getApplication().getMainView()).setLibro((Libro) val);
            }
        }
    }
    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        tfBuscar.requestFocus();
    }//GEN-LAST:event_formComponentShown

    private void tfBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfBuscarKeyReleased
        int i = tabla.getSelectedRow();
        if (evt.getKeyCode() == KeyEvent.VK_UP && i > 0) {
            tabla.getSelectionModel().setSelectionInterval(i - 1, i - 1);
            tabla.scrollRowToVisible(i - 1);
        } else if (evt.getKeyCode() == KeyEvent.VK_DOWN && i < tabla.getRowCount()) {
            tabla.getSelectionModel().setSelectionInterval(i + 1, i + 1);
            tabla.scrollRowToVisible(i + 1);
        } else if (evt.getKeyCode() == KeyEvent.VK_ENTER && i != -1) {
            cerrar();
        }
    }//GEN-LAST:event_tfBuscarKeyReleased

    private void formAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_formAncestorAdded
        buscador.buscar("_%");
    }//GEN-LAST:event_formAncestorAdded
    private BusquedaLibros buscador = new BusquedaLibros();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lBuscar;
    private org.jdesktop.swingx.JXTable tabla;
    private javax.swing.JTextField tfBuscar;
    // End of variables declaration//GEN-END:variables
}
